package com.lyf.viewapp;

import android.app.Activity;
import android.app.Application;
import android.os.Handler;

import com.lyf.viewapp.picture.show.loader.ImageDetailShowLoader;
import com.previewlibrary.ZoomMediaLoader;
import com.squareup.leakcanary.LeakCanary;

import java.util.LinkedList;
import java.util.List;

/**
 * Created by lyf on 2018/4/25 16:20
 *
 * @author lyf
 * desc：
 */
public class App extends Application {

    private static App mInstance;
    private List<Activity> mList = new LinkedList<>();

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
        ZoomMediaLoader.getInstance().init(new ImageDetailShowLoader());
        if (LeakCanary.isInAnalyzerProcess(this)) {
            // This process is dedicated to LeakCanary for heap analysis.
            // You should not init your app in this process.
            return;
        }
        LeakCanary.install(this);
        LeakCanary.install(this);
    }


    public static App getInstance() {
        if (mInstance == null) {
            synchronized (App.class) {
                mInstance = new App();
            }
        }
        return mInstance;
    }

    /**
     * 添加activity实列
     */
    public void addActivity(Activity activity) {
        mList.add(activity);
    }

    /***
     * 遍历退出
     */
    public void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null) {
                    activity.finish();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            }, 200);
        }
    }

}
